package com.example.school.controller.admin;

import com.example.school.dto.AdminGetListDto;
import com.example.school.dto.AdminSaveStudentDto;
import com.example.school.dto.AdminUpdateStudentDto;
import com.example.school.service.StudentService;
import com.example.school.utils.common.enums.Code;
import com.example.school.utils.common.result.Result;
import com.example.school.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/student")
public class AdminStudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping("/get_student_list")
    public Result getStudentList(@RequestBody AdminGetListDto dto){
        return studentService.getStudentList(dto);
    }

    @PostMapping("/save_student")
    public Result saveStudent(@RequestBody AdminSaveStudentDto dto){
        return studentService.saveStudent(dto);
    }

    @GetMapping("/remove_student")
    public Result removeStudent(@RequestParam("id") Long id){
        return studentService.removeStudent(id);
    }

    @GetMapping("/update_get_student")
    public Result updateGetStudent(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(studentService.getById(id));
    }

    @PostMapping("/updateStudent")
    public Result updateStudent(@RequestBody AdminUpdateStudentDto dto){
        return studentService.updateStudent(dto);
    }

}
